package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.doorjurisd.DoorManageActivity;
import com.example.jbmsuppliers.sinopec.doorjurisd.IdentificationStepOneActivity;
import com.example.jbmsuppliers.sinopec.doorjurisd.SensorService;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.SaveOpenKeyManage;
import com.jbm.jbmsupplier.util.IEGuardKey;
import com.jbm.jbmsupplier.util.IEGuardKeyCtrl;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 李征
 * Created by 家帮忙 on 2016/11/15.
 */

public class MyMabageDoor extends ActivityUse {
    @InjectView(R.id.layout_kaimenfenfa)
    LinearLayout layoutKaimenfenfa;
    @InjectView(R.id.layout_kaimenshouquan)
    LinearLayout layoutKaimenshouquan;
    @InjectView(R.id.layout_kaimenrenzheng)
    LinearLayout layoutKaimenrenzheng;
    @InjectView(R.id.layout_dizxinx)
    LinearLayout layoutDizxinx;
    @InjectView(R.id.swi_card_kaig_yy)
    SwitchCompat swiCardKaigYy;
    @InjectView(R.id.swi_card_kaig_xf)
    SwitchCompat swiCardKaigXf;
    private final int LOOKADDRESS = 0001;
    private final int AUTHORADDRESS = 0002;
    private final int DISTRADDRESS = 0003;
    private final int OPENDOORAUTHENT = 0004;
    private final int ISCHECK = 0005;
    private final int SEEKBARSENSOR = 0006;
    public static final int OPDE_DOORS = 0007;
    @InjectView(R.id.card_set_yy)
    TextView cardSetYy;
    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.titlelz)
    TextView titlelz;
    @InjectView(R.id.layout_lingmindutxt)
    TextView layoutLingmindutxt;
    private int intfz;
    private Bundle bundfz;
    private int i = 0;
    private SharedPreferences sharedPreferences, sharedPreferenceserson;
    private Intent floatintetnt, intentsensor;
    private ArrayList<IEGuardKey> ArrayListIEKey;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOOKADDRESS: {
                    setLooAddRess();
                    break;
                }
                case AUTHORADDRESS: {
                    setDoorAtchor();
                    break;
                }
                case DISTRADDRESS: {
                    setDistrMessage();
                    break;
                }
                case OPENDOORAUTHENT: {
                    setOpenDoorAuthent();
                    break;
                }
                case SEEKBARSENSOR: {
                    setSerSonSeek();
                    break;
                }
                case ISCHECK: {
                    boolean IsFitruexu = sharedPreferences.getBoolean("Isbudjexf", true);
                    boolean IsFitrueyy = sharedPreferences.getBoolean("Isbudjeyy", true);
                    opendoorkey = new SaveOpenKeyManage(MyMabageDoor.this).setgainOpenKey();
                    Log.e("main",IsFitruexu+"");
                    if (opendoorkey==null){
                        fb.setVisibility(View.GONE);
                        return;}
                    if (IsFitruexu == true) {
                        if(opendoorkey.size()>0){
                            fb.setVisibility(View.VISIBLE);
                       }
                        swiCardKaigXf.setChecked(IsFitruexu);
                    } else {
                        fb.setVisibility(View.GONE);
                        swiCardKaigXf.setChecked(IsFitruexu);
                    }
                    if (IsFitrueyy == true) {
                        if(opendoorkey.size()>0){
                        startService(intentsensor = new Intent(MyMabageDoor.this, SensorService.class));}
                        swiCardKaigYy.setChecked(IsFitrueyy);
                    } else {
                        if (intentsensor != null) {

                            stopService(intentsensor);
                        }
                        swiCardKaigYy.setChecked(IsFitrueyy);
                    }
                    setSwitchComp();
                    break;
                }
                case OPDE_DOORS: {
                    IEGuardKeyCtrl result = (IEGuardKeyCtrl) msg.obj;
                    ArrayListIEKey = new ArrayList<>();
                    if (result.back) {
                        try {
                            for (int i = 0; i < result.count; i++) {
                                IEGuardKey keyctrl = result.IndexofList(i);
                                ArrayListIEKey.add(keyctrl);
                            }
                            if (ArrayListIEKey.size() > 0) {
                                createShortCut();
                                fb.setVisibility(View.VISIBLE);
                            }
                            //保存用户开门key
                            new SaveOpenKeyManage(MyMabageDoor.this).setsaveOpenkey(ArrayListIEKey);
                            handler.sendMessage(handler.obtainMessage(ISCHECK));
                        } catch (Exception e) {
                        }

                    } else if (result.equals("time out")) {
                        handler.sendMessage(handler.obtainMessage(ISCHECK));
                    } else if (result.equals("Exception")) {
                        handler.sendMessage(handler.obtainMessage(ISCHECK));
                    }

                    break;
                }
            }

        }
    };
    private ArrayList<IEGuardKey> opendoorkey;

    @Override
    protected int setContentView1() {
        return R.layout.activity_door_show;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        titlelz.setText("手机开门设置");
        sharedPreferences = getSharedPreferences("dubje", Activity.MODE_PRIVATE);//来保存开关状态
        sharedPreferenceserson = getSharedPreferences("sersonzhi", Activity.MODE_PRIVATE);//来保存开关状态

        intfz = sharedPreferenceserson.getInt("serxon", 0);
        if (intfz > 0) {
            cardSetYy.setText("摇一摇灵敏度" + "(" + intfz + ")");
        } else {
            cardSetYy.setText("摇一摇灵敏度" + "(" + 1000 + ")");
        }
    }
    @OnClick({R.id.layout_kaimenfenfa, R.id.layout_kaimenshouquan, R.id.layout_kaimenrenzheng, R.id.layout_dizxinx, R.id.card_set_yy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_kaimenrenzheng:
                handler.sendMessage(handler.obtainMessage(OPENDOORAUTHENT));
                break;
            case R.id.layout_kaimenfenfa:
                handler.sendMessage(handler.obtainMessage(DISTRADDRESS));
                break;
            case R.id.layout_kaimenshouquan:
                handler.sendMessage(handler.obtainMessage(AUTHORADDRESS));
                break;
            case R.id.card_set_yy:
                handler.sendMessage(handler.obtainMessage(SEEKBARSENSOR));
                break;
            case R.id.layout_dizxinx:
                handler.sendMessage(handler.obtainMessage(LOOKADDRESS));
                break;
        }
    }


    private void setLooAddRess() {//地址信息
        startActivity(new Intent(this, MyAddressAll.class).putExtra("dizhi", "dizhi"));
    }

    private void setDoorAtchor() {//授权信息
        startActivity(new Intent(this, DoorManageActivity.class).putExtra("shouquan", "shouquan"));
    }

    private void setDistrMessage() {//分发信息
        startActivity(new Intent(this, MyDoorManageF.class).putExtra("fenfa", "fenfa"));
    }

    private void setOpenDoorAuthent() {//卡号认证
        startActivity(new Intent(this, IdentificationStepOneActivity.class));
    }

    private void setSwitchComp() {
        swiCardKaigXf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPreferences.edit().putBoolean("Isbudjexf", isChecked).commit();
//                    startService(new Intent(MyMabageDoor.this, FloatWindowService.class));
                    if (opendoorkey==null){
                        fb.setVisibility(View.GONE);
                        return;}
                    if(opendoorkey.size()>0){
                        fb.setVisibility(View.VISIBLE);
                    }

                } else {
                    sharedPreferences.edit().putBoolean("Isbudjexf", isChecked).commit();
                    if(opendoorkey.size()>0){
                        fb.setVisibility(View.GONE);
                    }

//                    stopService(new Intent(MyMabageDoor.this, FloatWindowService.class));

                }
            }
        });
        swiCardKaigYy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPreferences.edit().putBoolean("Isbudjeyy", isChecked).commit();
                    startService(new Intent(MyMabageDoor.this, SensorService.class));
                } else {
                    sharedPreferences.edit().putBoolean("Isbudjeyy", isChecked).commit();
                    stopService(new Intent(MyMabageDoor.this, SensorService.class));


                }
            }
        });

    }

    private void setSerSonSeek() {//调节摇一摇的敏感度
        View view = View.inflate(this, R.layout.activity_sensor_seekbar, null);
        final Dialog alertdialog = new Dialog(MyMabageDoor.this, R.style.baise);
        alertdialog.setContentView(view);
        Window windowmess = alertdialog.getWindow();
        WindowManager.LayoutParams attri = windowmess.getAttributes();
        attri.alpha = 0.5f;
        windowmess.setAttributes(attri);
        alertdialog.show();
        final RelativeLayout linserson = (RelativeLayout) view.findViewById(R.id.sersonlinear);
        final TextView sersongao = (TextView) view.findViewById(R.id.serson_zhi);
        final SeekBar seekbarSensorFanzhishudu = (SeekBar) view.findViewById(R.id.seekbar_sensor_fanzhishudu);
        intfz = sharedPreferenceserson.getInt("serxon", 0);
        if (intfz > 0) {
            seekbarSensorFanzhishudu.setMax(3000);
            seekbarSensorFanzhishudu.setProgress(intfz);
            sersongao.setText(intfz + "");
            cardSetYy.setText("摇一摇灵敏度" + "(" + intfz + ")");

        } else {
            seekbarSensorFanzhishudu.setMax(3000);
            seekbarSensorFanzhishudu.setProgress(1000);

            sersongao.setText(1000 + "");
        }
        seekbarSensorFanzhishudu.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //移动的时候触发的监听
                sersongao.setText(progress + "");
                if (progress < 200) {
                    seekbarSensorFanzhishudu.setProgress(200);
                    return;
                } else {
                    bundfz = new Bundle();
                    bundfz.putInt("ser_fz", progress);
                    cardSetYy.setText("摇一摇灵敏度" + "(" + progress + ")");
                    i = 1;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (i == 1) {
                    if (alertdialog != null) {
                        alertdialog.dismiss();
                    }
                    sharedPreferenceserson.edit().putInt("serxon", bundfz.getInt("ser_fz")).commit();
                    i = 0;
                } else {
                    return;
                }
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        NetWork netDoorManager = new NetWork(handler);
        netDoorManager.getDoorKeyManageS();

    }

    @OnClick(R.id.goback_ps_set)
    public void onClick() {
        finish();
    }
}
